<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>GPS Data Store</title>
    <link href="StyleSheet.css" rel="stylesheet" type="text/css" />

    <script src="http://dev.virtualearth.net/mapcontrol/v3/mapcontrol.js" type="text/javascript"></script>

    <script type="text/javascript">
//<![CDATA[	
//
// Copyright (c) Microsoft Corporation.  All rights reserved.
//

	// Map object
	var map = null;
	
	// Set units on Map Scale Bar
	function SetUnits()
	{
	    if (document.getElementById("unitKilometers").checked)
	    {
	        map.SetScaleBarDistanceUnit(VEDistanceUnit.Kilometers);
	    }
	    else
	    {
	        map.SetScaleBarDistanceUnit(VEDistanceUnit.Miles);
	    }
	}
	
	// Map inicialization
	function PrepareMap()
	{
	    map = new VEMap('mapa');
	    map.LoadMap(new VELatLong(50.04780278, 14.45501111), // Map center coordinates
	        15, // zoom 1 - 19
	        VEMapStyle.Road,
	        false);
	    
	    SetUnits();
	}
	
	// Method reads trip coordinates from server
	function ShowTrip()
	{
	    document.getElementById('progressbar').style.visibility = "visible";
	    
	    var request = new ActiveXObject("Microsoft.XMLHTTP");
        
        var combo = document.getElementById("cboTrip")
        request.onreadystatechange = readData;
                 
        request.open('GET', "GetGpx.ashx?id=" + combo.options[combo.selectedIndex].value, true);
        request.send(null);
        
        function readData()
        {
            if (request.readyState != 4)
            {
                return;                
            }
            
            if (request.status != 200)
            {
                alert("Server Error: " + request.status);
                return;
            }
            
            var xml = request.responseXML;
            
             var xml = request.responseXML;
            
            var items = xml.getElementsByTagName('rtept');
            
            var points = new Array();
            
           
            for (i = 0; i < items.length; i++ )
            {
                var item = items.item(i);
                points[i] = new VELatLong(item.getAttribute('lat'),
                                        item.getAttribute('lon'));
            }
            
            var poly = new VEPolyline('route', points);
            poly.SetWidth(2); 
		    poly.SetColor(new VEColor(250, 50, 50, 1.0));
		    
		    if (map == null)
		    {
		        PrepareMap();
		    }
		    
		    map.DeleteAllPolylines();
		    map.AddPolyline(poly);
		    
		    map.DeleteAllPushpins();
		    items = xml.getElementsByTagName('wpt');
           
            for (i = 0; i < items.length; i++ )
            {
                var item = items.item(i);
                var pin = new VEPushpin(i,
                                new VELatLong(item.getAttribute('lat'), item.getAttribute('lon')),
                                null,
                                'Waypoint',
                                '<strong>Time: </strong>' + item.getElementsByTagName('time')[0].firstChild.nodeValue + '<br>' +
                                '<strong>Altitude: </strong>' + item.getElementsByTagName('ele')[0].firstChild.nodeValue);
                map.AddPushpin(pin);
            }
		    
		    
		    if (points.length > 0)
		    {
		        map.SetCenter(points[0]);
		    }
    	    document.getElementById('progressbar').style.visibility = "hidden";	    
        }
	}
// ]]>	 
    </script>

</head>
<body onload="PrepareMap();">
    <form id="form1" runat="server">
        <atlas:ScriptManager ID="ScriptManager1" runat="server" EnablePartialRendering="true" />
        <h1>
            GPS Tracker</h1>
        <div>
            [<a href="UserArea/Default.aspx">Upload coordinates.</a>], [<asp:LoginStatus ID="LoginStatus1" runat="server" />]
        </div>
        <table>
            <tr>
                <td style="width: 100px">
                    User:
                </td>
                <td style="width: 100px">
                    <asp:DropDownList ID="cboUser" runat="server" AutoPostBack="True" DataSourceID="SqlDataSource1"
                        DataTextField="UserName" DataValueField="UserId" Width="200">
                    </asp:DropDownList><asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:mainConnection %>"
                        SelectCommand="SELECT [UserName], [UserId] FROM [vw_aspnet_Users] ORDER BY [UserName]">
                    </asp:SqlDataSource>
                </td>
                <td rowspan="2">
                    <a href="http://www.codeplex.com/Wiki/View.aspx?ProjectName=gpstracker ">
                        <img src="http://www.codeplex.com/Project/FileDownload.aspx?ProjectName=CodePlex&DownloadId=1743"
                            alt="CodePlex" style="border-width: 0pt;" /></a>
                </td>
                <td rowspan="2">
                    Distance unit:<br />
                    <input id="unitKilometers" name="unit" type="radio" value="kilometers" checked="checked"
                        onclick="SetUnits();" />Kilometers<br />
                    <input id="unitMiles" name="unit" type="radio" value="miles" onclick="SetUnits();" />Miles
                </td>
            </tr>
            <tr>
                <td style="width: 100px">
                    Trip:
                </td>
                <td style="width: 100px">
                    <atlas:UpdatePanel runat="server" ID="updateTripList" Mode="Conditional">
                        <Triggers>
                            <atlas:ControlEventTrigger ControlID="cboUser" EventName="SelectedIndexChanged" />
                        </Triggers>
                        <ContentTemplate>
                            <asp:DropDownList ID="cboTrip" runat="server" DataSourceID="SqlDataSource2" DataTextField="Name"
                                DataValueField="GpxGuid" Width="200px">
                            </asp:DropDownList><asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:mainConnection %>"
                                SelectCommand="SELECT [Name], [GpxGuid] FROM  [gpsdatastore].[Gpx] WHERE ([UserId] = @UserId) UNION SELECT '[Choose trip]' as [Name], '00000000-0000-0000-0000-000000000000' as [GpxGuid] ORDER BY [Name]">
                                <SelectParameters>
                                    <asp:ControlParameter ControlID="cboUser" Name="UserId" PropertyName="SelectedValue"
                                        Type="String" />
                                </SelectParameters>
                            </asp:SqlDataSource>
                        </ContentTemplate>
                    </atlas:UpdatePanel>
                </td>
            </tr>
            <tr>
                <td colspan="4" align="center">
                    <img src="images/progressbar_long_green.gif" alt="Working..." id="progressbar" style="visibility: hidden;" />
                </td>
            </tr>
        </table>
        <div id="mapa" style="width: 640px; height: 480px;">
        </div>
    </form>
</body>
</html>
